package net.hn.hnms.biz.equipment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.jsonwebtoken.lang.Collections;
import net.hn.hnms.biz.equipment.constant.Dict;
import net.hn.hnms.biz.equipment.domain.CoalMineSysPointPo;
import net.hn.hnms.biz.equipment.mapper.CoalMineSysPointMapper;
import net.hn.hnms.biz.equipment.service.CoalMineSysPointService;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import net.hn.hnms.sys.common.mybatis.core.page.TableDataInfo;
import net.hn.hnms.sys.system.api.RemoteDictService;
import net.hn.hnms.sys.system.api.domain.vo.SysDictDataTypeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class CoalMineSysPointServiceImpl implements CoalMineSysPointService {
    @Resource
    private CoalMineSysPointMapper mineSysPointMapper;

    @Autowired
    private RemoteDictService remoteDictService;
    List<SysDictDataTypeVo> dictTypeList = null;
    List<SysDictDataTypeVo> dictUalityList = null;

    /**
     * 功能描述: 获取编码配置列表
     *
     * @param
     * @return void
     * @auther: WangLong
     * @date: 2022/7/28 10:47
     */

    @Override
    public List<CoalMineSysPointPo> getList() {
        List<CoalMineSysPointPo> coalMineSysPoints = mineSysPointMapper.selectList(null);
        if (Collections.isEmpty(coalMineSysPoints)) {
            return new ArrayList<>();
        }
        return coalMineSysPoints;
    }

    /**
     * 功能描述: 条件查询编码配置列表分页
     *
     * @param
     * @return java.util.List<net.hn.hnms.biz.equipment.domain.CoalMineSysPoint>
     * @auther: WangLong
     * @date: 2022/7/28 11:04
     */

    @Override
    public TableDataInfo<CoalMineSysPointPo> pageList(PageQuery pageQuery, CoalMineSysPointPo coalMineSysPoint) {
        Page page1 = mineSysPointMapper.selectPage(pageQuery.build(), new QueryWrapper<>(coalMineSysPoint));
        return TableDataInfo.build(page1);
    }

    /**
     * 功能描述: 根据设备名称查询配置表
     *
     * @param category
     * @return java.util.List<net.hn.hnms.biz.equipment.domain.CoalMineSysPoint>
     * @auther: WangLong
     * @date: 2022/7/28 14:42
     */

    @Override
    public List<CoalMineSysPointPo> getCategoryList(String category) {
        List<CoalMineSysPointPo> api_categoryList = mineSysPointMapper.selectList(new QueryWrapper<CoalMineSysPointPo>().eq("api_category", category));
        return api_categoryList;
    }

    /**
     * 功能描述: 根据测点数据名称和设备名称设置测点编码
     *
     * @param apiName
     * @param apiCategory
     * @param point
     * @return java.lang.Boolean
     * @auther: WangLong
     * @date: 2022/7/28 15:18
     */

    @Override
    public Boolean setPoint(String apiName, String apiCategory, String point) {
        CoalMineSysPointPo one = getOne(apiName, apiCategory);
        one.setPointCode(point);
        int i = mineSysPointMapper.updateById(one);
        if (i > 0) {
            return true;
        }
        return false;
    }

    /**
     * 功能描述: 新增测点编码记录
     *
     * @param coalMineSysPoint
     * @return java.lang.Boolean
     * @auther: WangLong
     * @date: 2022/7/29 11:24
     */

    @Override
    public Boolean add(CoalMineSysPointPo coalMineSysPoint) {
        int insert = mineSysPointMapper.insert(coalMineSysPoint);
        if (insert > 0) {
            return true;
        }
        return false;
    }

    /**
     * 功能描述: 根据测点数据名称和设备名称查询记录
     *
     * @param apiName
     * @param apiCategory
     * @return coalMineSysPoint
     * @auther: WangLong
     * @date: 2022/7/28 15:20
     */
    public CoalMineSysPointPo getOne(String apiName, String apiCategory) {
        CoalMineSysPointPo coalMineSysPoint = mineSysPointMapper.selectOne(new QueryWrapper<CoalMineSysPointPo>().eq("api_name", apiName).eq("api_category", apiCategory));
        return coalMineSysPoint;
    }


    //远程调用字典，根据字典类型获取字典列表
    public List<SysDictDataTypeVo> getDictList(String dictType) {
        return remoteDictService.getDictList(dictType);
    }
    /**
     * 数值类型转换 MN转化为模拟量
     * @param valueType
     * @return
     */
    public String valueTypeTransitionString(String valueType) {
        if (dictTypeList == null) {
            //如果字典为空，则远程调用
            dictTypeList = this.getDictList(Dict.BIZ_EQUIPMENT_TYPE);
        }
//        System.out.println( "--------获取值为“valueType”的值，如果没有找到则返回null--------");
        SysDictDataTypeVo sysDictDataTypeVo = dictTypeList.stream().filter(t -> valueType.equals(t.getDictValue())).findAny().orElse(new SysDictDataTypeVo());
        String dictLabel = sysDictDataTypeVo.getDictLabel();
        if (dictLabel==null) {
            sysDictDataTypeVo.setDictLabel("---");
        }
        return sysDictDataTypeVo.getDictLabel();
    }

    /**
     * 数据质量类型转换  1 转为 Good
     * @param valueType
     * @return
     */
    public String dataUalityTransitionString(Integer valueType) {
        if (dictUalityList==null) {
            dictUalityList=this.getDictList(Dict.BIZ_EQUIPMENT_QUALITY);
            //如果字典为空，则远程调用
        }
//        System.out.println( "--------获取值为“valueType”的值，如果没有找到则返回null--------");
        SysDictDataTypeVo sysDictDataTypeVo = dictUalityList.stream().filter(t -> valueType==Integer.parseInt(t.getDictValue())).findAny().orElse(new SysDictDataTypeVo());
        String dictLabel = sysDictDataTypeVo.getDictLabel();
        if (dictLabel==null) {
            sysDictDataTypeVo.setDictLabel("---");
        }
        return sysDictDataTypeVo.getDictLabel();
    }



}
